package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //setmeal表  保存套餐的基本信息 此步获取套餐id在下面循环使用
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //setmeal_dish表  保存套餐和菜品的关联信息
        setmealDishService.saveBatch(setmealDishes);
    }



    /**
     * 删除套餐，同时需要删除套餐和菜品的 关联 数据(菜品不删除)  套餐的在售状态
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //查询售卖状态 确认是否可以删除
        //select count(*) from setmeal where id in (ids) and status = 1
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);
        qw.eq(Setmeal::getStatus,1);

        int count = this.count(qw);
        if (count>0){
            //不能删除 抛出业务异常
            throw new CustomException("套餐正在售卖中,不能删除");
        }
        //删除setmeal表数据
        this.removeByIds(ids);
        //delete from setmeal_dish where setmeal_id in (ids)
        LambdaQueryWrapper<SetmealDish> qw2 = new LambdaQueryWrapper<>();
        qw2.in(SetmealDish::getSetmealId,ids);
        //删除setmeal_dish表数据  根据setmeal_id字段不是id
        setmealDishService.remove(qw2);
    }



    @Override
    public void changeStatus(List<Long> ids, Integer status) {
        //update setmeal set status = (status) where id in (ids)
        LambdaUpdateWrapper<Setmeal> qw = new LambdaUpdateWrapper<>();
        qw.set(Setmeal::getStatus,status).in(Setmeal::getId,ids);

        this.update(qw);

    }

    //查询套餐id 关联表 得到套餐 的菜品
    @Override
    public SetmealDto getSetmealById(Long id) {
        //select * from setmealdish where setmealid = ?
        Setmeal setmeal = this.getById(id);

        SetmealDto setmealDto = new SetmealDto();

        BeanUtils.copyProperties(setmeal,setmealDto);

        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,setmeal.getId());

        List<SetmealDish> list = setmealDishService.list(qw);

        setmealDto.setSetmealDishes(list);
        return setmealDto;

    }

    /**
     * 修改套餐   关系表是先删除再增加
     * @param setmealDto
     */
    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        //setmeal直接按id修改内容
        this.updateById(setmealDto);
        //根据setmealDto/setmeal 的 id 删除 setmealDish 关系表 的数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishService.remove(queryWrapper);
        //从套餐类获取菜品集合  对象的setmealId字段为null
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        //每个菜品设置套餐的id
        List<SetmealDish> setmealDishList = list.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //根据每条数据 保存在setmeal_dish表
        setmealDishService.saveBatch(setmealDishList);
    }
}
